package com.learn.sang.xmlparser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import com.sang.database.CiteseerDb;

public class SaxTest {

	private int numOfRecords = 716800;

	private int authorId = numOfRecords + 1; // author id start from 716800
	private HashMap<String, Integer> authors = new HashMap<String, Integer>();
	
	private static String xmlfile = "./resource/a.xml";//sample_data.xml";

	private String pathOfPaperInfo = "./resource/paperInfo.txt";
//	private String pathOfDescriotion = "./resource/description.txt";
	private String pathOfAuthorInfo = "./resource/author.txt";
	private String pathOfRelation = "./resource/relation.txt";

//	private BerkelyDb bdb;
//	private String dbName = "author";

	public SaxTest() {

	}

	public void initDB() {
//		bdb = BerkelyDb.newInstance();
		// bdb.openDb("author");
	}
	
	public static void main(String[] args){
		SaxTest st = new SaxTest();
		st.parseXML(new File(xmlfile));
	}
	
	

	public void parseXML(File xmlFile) {
		InputStream is = null;
		XMLStreamReader xsr = null;
		XMLInputFactory xif = XMLInputFactory.newInstance();

		String tag = null;
		String mark = null;

		String paperInfo = null;// 记录paper的信息
		String description = null;// 记录paper的摘要
		String authorInfo = null;// 记录author的信息
		
		int maxsize = 400;
		StringBuffer paperToFile = new StringBuffer(maxsize);//把paper相关信息内容写入文件
		StringBuffer authorToFile = new StringBuffer(maxsize);//把author相关信息写入文件
		StringBuffer relationToFile = new StringBuffer(maxsize);//把paper author 间的关系写入文件
		
		FileWriter paperFw = null;
		FileWriter authorFw = null;
		FileWriter relationFw = null;

		try {
			is = new FileInputStream(xmlFile);
			xsr = xif.createXMLStreamReader(is);
			
			paperFw = new FileWriter(pathOfPaperInfo);
			authorFw = new FileWriter(pathOfAuthorInfo);
			relationFw = new FileWriter(pathOfRelation);

			String name = "";
			int nameExist = 0;
			String address = "";

			for (int event = xsr.next(); event != XMLStreamReader.END_DOCUMENT; event = xsr
					.next()) {
				switch (event) {
				case XMLStreamReader.START_ELEMENT:
					tag = xsr.getLocalName();
					if (tag.equals("oai_citeseer:oai_citeseer")) {// 从这个标签开始的xml部分是重点
						paperInfo = "";
						description = "";
						
					} else if (tag.equals("identifier")) {

					} else if (tag.equals("dc:title")) {
						authorInfo = xsr.getElementText().trim();

					} else if (tag.equals("oai_citeseer:author")) {
						authorInfo = "type:person@@name:"+ xsr.getAttributeValue(0).trim();
					} else if (tag.equals("address")) {
						authorInfo += "@@address:" + xsr.getElementText().trim();
						if (!authors.containsKey(authorInfo)) {
							authors.put(authorInfo, authorId++);
							nameExist = 0;
						} else {
							nameExist = 1;
						}
					} else if (tag.equals("affiliation")) {
						if(nameExist == 0){
							authorInfo += "@@affiliation:" + xsr.getElementText().trim();
						}
					} else if (tag.equals("dc:description")) {

					} else if (tag.equals("dc:date")) {

					} else if (tag.equals("dc:source")) {

					}
				case XMLStreamReader.END_ELEMENT:
					tag = xsr.getLocalName();
					if (tag.equals("oai_citeseer:author")) {
						if(authorToFile.capacity()> maxsize){
							authorFw.append(authorToFile.toString());
							authorToFile = new StringBuffer(maxsize);
						}
						authorToFile.append(authorId+"\t"+authorInfo+"\n");
					}

				case XMLStreamReader.CHARACTERS:
					// title += xsr.getText().replace('\n', ' ').trim();
					// break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				xsr.close();
				is.close();
				paperFw.close();
				authorFw.close();
				relationFw.close();
			} catch (IOException ex) {
				System.err.println("error in closing io");
			} catch (XMLStreamException ex) {
				System.err.println("error in closing xsr");
			}
		}
	}

	public void parseXML(String foldPath) {

	}
}
